//
//  PPTabBarController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/23.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "PPTabBarController.h"
#import "PPHomeViewController.h"

#import "IncomeViewController.h"
#import "AuctionViewController.h"
#import "PPCategoryViewController.h"
#import "TempCategoryViewController.h"

#import "PPMineViewController.h"

#import "PaingSocketViewController.h"

#import "PPNavigationViewController.h"

@interface PPTabBarController ()<UINavigationControllerDelegate,UITabBarControllerDelegate>

@end

@implementation PPTabBarController

+ (instancetype)shareTabbar {
    
    static PPTabBarController *shareTab = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!shareTab) {
            shareTab = [[self alloc] init];
        }
    });
    return shareTab;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    [self uiConfig];
    
    [self.tabBar setBackgroundImage:[UIImage new]];
    [self.tabBar setShadowImage:[self imageWithColor:UIColorHex(0xd9d9d9) size:CGSizeMake(TTScreenWidth, 0.5)]];
    
    self.tabBar.translucent = NO;
    [self.tabBar setBarTintColor:[UIColor whiteColor]];
    
}

+ (void)setCurrentIndex:(PPTabbarIndexType)indexType {
    
    [Utilities tapticEngineWork];
    [[PPTabBarController shareTabbar] setSelectedIndex:indexType];
}

- (void)uiConfig
{
    _homeNav = [self viewControllerWithTabTitle:@"首页"
                                          image:@"home_nor"
                                  selectedImage:@"home_sel"
                                      className:@"PPHomeViewController"];
    
    _auctionNav = [self viewControllerWithTabTitle:@"拍品"
                                             image:@"auction_nor"
                                     selectedImage:@"auction_sel"
                                         className:@"AuctionViewController"];
    
    _shopMallNav = [self viewControllerWithTabTitle:@"拍拍生活"
                                              image:@"store_nor"
                                      selectedImage:@"store_sel"
                                          className:@"TempCategoryViewController"];
    
    _incomeNav = [self viewControllerWithTabTitle:@"收益"
                                            image:@"income_nor"
                                    selectedImage:@"income_sel"
                                        className:@"IncomeViewController"];
    
    _mineNav = [self viewControllerWithTabTitle:@"我的"
                                          image:@"mine_nor"
                                  selectedImage:@"mine_sel"
                                      className:@"PPMineViewController"];
    
    self.viewControllers = @[_homeNav, _auctionNav, _shopMallNav, _incomeNav, _mineNav];
}

//默认图片设置以及选中状态图片设置
- (UINavigationController *)viewControllerWithTabTitle:(NSString*)title image:(NSString *)image selectedImage:(NSString *)selectedImage className:(NSString *)className
{
    Class cls = NSClassFromString(className);
    UIViewController *viewController = [[cls alloc] init];
    
    UIImage *Image = [UIImage imageNamed:image];
    UIImage *ImageSel = [UIImage imageNamed:selectedImage];
    
    Image = [Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    ImageSel = [ImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:Image selectedImage:ImageSel];

//    viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    
    
    self.tabBar.tintColor= PP_Default_Color_Red_FF3737;
    
    PPNavigationViewController *navController = [[PPNavigationViewController alloc] initWithRootViewController:viewController];
    navController.navigationBar.translucent = NO;
    

    navController.navigationBar.barTintColor = [UIColor whiteColor];
    navController.delegate = self;
    return navController;

}

//根据颜色生成图片的函数

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, color.CGColor);
    
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}

#pragma mark - UITabBarControllerDelegate
//- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
//    __block BOOL select = YES;
//    NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
//
//
//
//    if (index == 2 || index == 3) { //判断登录状态
//        if ([TTUserModel sharedObject].isLogged) {
//            select = YES;
//        }else {
//            @weakify(self)
//            [self.selectedViewController openLoginWithCompletion:^(BOOL isLogin,NSDictionary *options) {
//                if (isLogin) {
//                    [weak_self setSelectedIndex:index];
//                }
//            }];
//            select = NO;
//        }
//    }
//    return select;
//}


@end
